<template>
  <div class="all-page" id="building">
    <div class="login-container" id="method-background">
      <!-- 二维码登录部分 -->
      <div class="qr-login">
        <p style="text-align: center;margin-top: 0;margin-bottom:5px;font-size: 30px;color: #1e9a1e">智慧能源平台</p>
        <img src="../assets/logo.png" alt="智慧能源平台logo" class="qr-code">
        <!-- 假设有一个模态框用于显示二维码的详细信息，这里只是示例 -->
        <div v-if="showQrCodeModal" class="qr-code-modal">
          <img src="../assets/logo.png" class="qr-code-modal-img" alt="登录二维码">
          <button @click="showQrCodeModal = false" class="close-button">×</button>
          <p style="font-size:20px;display: flex;justify-content: center;color:#8c8a8a; ">扫码进行登录</p>
        </div>
      </div>

      <!-- 账号和短信登录部分 -->
      <div class="login-methods">
        <!-- 登录方式切换按钮 -->
        <div class="login-method-buttons">
          <button @click="selectLoginMethod('password')" :class="{ active: selectedLoginMethod === 'password' }" style="border: none;background-color: white;font-size: 20px; cursor: pointer; /* 添加点击手型 */">账号登录</button>
          <el-divider direction="vertical"></el-divider>
          <button @click="selectLoginMethod('sms')" :class="{ active: selectedLoginMethod === 'sms' }" style="border: none;background-color: white;font-size: 20px; cursor: pointer; /* 添加点击手型 */">短信登录</button>
        </div>

        <!-- 根据选择的登录方式显示对应的表单 -->
        <div class="login-form" v-if="selectedLoginMethod === 'password'">
          <div class="input-container">
            <span class="input-prefix">账号</span>
            <input type="text" v-model="userName" placeholder="请输入内容" style="border: 0;outline: 0">
          </div>
          <div class="input-container">
            <span class="input-prefix">密码</span>
            <input type="text" v-model="password" placeholder="请输入密码" style="border: 0;outline: 0">
          </div>
          <div>
            <el-button @click="reset" class="login-button" >重置</el-button>
            <el-button @click="handleLogin('password')" class="login-button" style="float: right;background-color: #7bd5f3">登录</el-button>
          </div>
        </div>

        <div class="login-form" v-if="selectedLoginMethod === 'sms'">
          <div class="input-container">
            <span class="input-prefix">手机号</span>
            <input type="text" v-model="phone" placeholder="请输入手机号" style="border: 0;outline: 0">
          </div>
          <div class="input-container">
            <span class="input-prefix">验证码</span>
            <input type="text" v-model="smsCode" placeholder="请输入验证码" style="border: 0;outline: 0">
          </div>
          <el-button @click="sendSmsCode" class="login-button">发送验证码</el-button>
          <el-button @click="handleLogin('sms')" class="login-button" style="float: right;background-color: #82dbf8">登录</el-button>
        </div>
      </div>
      <div class="other-login-text">
        <p style="color: #8c8a8a;margin-right: 26px">其他方式登录</p>
        <el-button icon="el-icon-food" style="color: #8c8a8a;border: 0;float: left;padding: 0;margin-left: 20px" @click="showQrCodeModal = true" >微信登录</el-button>
        <el-button icon="el-icon-food" style="color: #8c8a8a;border: 0;padding: 0">微信登录</el-button>
        <el-button icon="el-icon-food" style="color: #8c8a8a;border: 0;float: right;padding: 0;margin-right: 50px">微信登录</el-button>
      </div>
      <div class="terms">
        <p style="margin-bottom: 1px;color: #8c8a8a">欢迎使用智慧能源管理平台</p>
        <p style="margin-top: 1px;color: #8c8a8a">登录即代表你同意用户协议和隐私政策</p>
      </div>

    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      showQrCodeModal: false,
      selectedLoginMethod: 'password', // 默认选择账号登录
      userName: '',
      password: '',
      phone: '',
      smsCode: '',
      // 其他需要的状态，如验证码发送状态、倒计时等
    };
  },
  methods: {
    selectLoginMethod(method) {
      this.selectedLoginMethod = method;
      // 可以在这里重置表单字段，例如：
      if (method === 'password') {
        this.userName = '';
        this.password = '';
      } else if (method === 'sms') {
        this.phone = '';
        this.smsCode = '';
        // 停止任何正在进行的验证码倒计时
      }
    },
    async handleLogin(method) {
      // if (method === 'password') {
      //   // 处理账号登录逻辑
      //   const params = {
      //     userName: this.userName,
      //     password: this.password
      //   }
      //   const response = await loByUserName(params);
      //   const res = response.data;
      //   if (res.code ===20000){
      //     this.$message.success("登陆成功，欢迎回来！")
      //     setTimeout(() => {
      //       this.$router.push({path: '/section-one/home'});
      //     }, 200);
      //   }else {
      //     this.$message.error("账号或密码错误，请重新登录！")
      //   }
      // } else if (method === 'sms') {
      //   // 处理短信登录逻辑
      //   const response = await loByCode(this.phone,this.smsCode);
      //   const res = response.data;
      //   if (res.code ===20000){
      //     this.$message.success("登陆成功，欢迎回来！");
      //     setTimeout(() => {
      //       this.$router.push({path: '/section-one/home'});
      //     }, 200);
      //   }else if(res.code ===6){
      //     this.$message.error("验证码错误！")
      //   }else {
      //     this.$message.error("用户无权登录此平台！")
      //   }
      // }
      if (method === 'password'){
        if (this.userName === 'admin' && this.password === '123456'){
          await this.$router.push({path: '/section-one/home'});
        }else {
          this.$message.error("账号或密码错误，请重新登录！")
        }
      }else if (method === 'sms') {
        if (this.phone === '1234567890' && this.smsCode === '666'){
          await this.$router.push({path: '/section-one/home'});
        } else {
          this.$message.error("验证码错误！")
        }
      }


    },
    async sendSmsCode() {
      // if (this.phone === null||this.phone === ''){
      //   this.$message.error("请填写电话号码！")
      // }else {
      //   // 发送验证码到指定的手机号
      //   const response = await sendCode();
      //   const res = response.data;
      //   if (res.code === 20000){
      //     this.$message.success("验证码已发送!")
      //   }else {
      //     this.$message.error("发送失败！")
      //   }
      // }

      this.$message.success("验证码已发送!")

    },
    reset(){
      this.userName='';
      this.password='';
    }


  },

  created() {
    // getAccount().then(res=>{
    //   console.log(res);
    // })
  }

};
</script>

<style scoped>
.all-page{
  margin: 0;
  height: 100%;
  width: 100%;
  background: #7bd5f3;
}

#building {
  background: url("../assets/login-b2.jpg") no-repeat center center fixed;
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: cover; /* 确保图片覆盖整个屏幕 */
  top: 0; /* 确保元素从屏幕顶部开始 */
  left: 0; /* 确保元素从屏幕左侧开始 */
}

.login-container {
  position: absolute;
  opacity: 1;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 5px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  width: 650px;
  height: 315px;
  background-color: white;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#method-background{
  background: url("../assets/login-method-background.png") no-repeat;
  background-size: cover;
}

.qr-login {
  margin-top: 20px;
  margin-left: 20px;
  margin-right: 20px;
  float: left;
  width: 30%;
  padding-right: 24px;
  border-right: 1px solid #ccc; /* 可选：为前缀和输入框间添加分隔线 */
}

.qr-code {
  margin: 0 auto; /* 水平居中 */
  width: 190px;
  height: 190px;
  display: block;
}

.qr-code-modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: white;
  border: 1px solid #ccc;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
  z-index: 1000; /* 确保模态框在最上层 */
}

.qr-code-modal-img {
  margin-top: 40px;
  margin-left: 32px;
  width: 200px;
  height: 200px;
}

.close-button{
  margin-top: 0;
  margin-right: 0;
  float: right;
  border: 0;
  font-size: 35px;
  background-color: white;
  cursor: pointer;
}

.login-methods {
  margin-top: 22px;
  margin-left: 270px;
  flex: 1;
  height: 150px;
}

.login-method-buttons {
  margin-bottom: 25px;
  margin-left: 60px
}

.active {
  font-weight: bold;
  color: #48b2bd; /* 假设的活跃按钮颜色 */
}

.terms{
  margin-top: 25px;
  text-align: center;
  font-size: 12px;
}
.login-form{
  width: 340px;
}
.input-container {
  display: flex;
  align-items: center;
  border: 1px solid #c9c8c8; /* 2px宽的黑色实线边框 */
  padding: 10px;
  width: 320px;
}

.input-prefix {
  margin-right: 8px; /* 可根据需要调整间距 */
  background-color: #fff; /* 确保背景色与输入框一致，以实现视觉上的融合 */
  padding: 0 4px; /* 可根据需要添加内边距 */
  border-right: 1px solid #ccc; /* 可选：为前缀和输入框间添加分隔线 */
  pointer-events: none; /* 防止点击事件作用于前缀文本 */
  user-select: none; /* 禁止用户选择前缀文本 */
}
.login-button{
  margin-top: 15px;
  padding: 10px;
  background-color: white;
  width: 160px;
  border: 1px solid #ccc; /* 可选：为前缀和输入框间添加分隔线 */
}
.other-login-text{
  margin-top: 50px;
  font-size: 12px;
  text-align: center;
}

/* 其他样式... */
</style>
